home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / CTEX__ / GFREAD.C next >
Text File  |  1987-05-25  |  15KB  |  491 lines

  1. /* GFread in C---Tomas Rokicki */ 
  2. #include <stdio.h>
  3. #define linelength 79 
  4. #define terminallinelength 150 
  5. #define memmax 4000 
  6. #define incr(a) a++
  7. #define decr(a) a--
  8. #define true (1)
  9. #define false (0)
  10. #define round(a) ((int)(a+.5))
  11. #define ptc(a,b) putc((char)(a),b)
  12.  
  13.   typedef int integer ;
  14.   typedef short boolean ;
  15.   typedef FILE *bytefile ; 
  16.   bytefile gffile ; 
  17.   bytefile pxlfile ; 
  18.   integer pxlloc ; 
  19.   char gfname[81], pxlname[81] ; 
  20.   integer mem[4001] ; 
  21.   integer power[17] ; 
  22.   integer gfch ; 
  23.   integer checksum ; 
  24.   integer dirptr ; 
  25.   integer designsize ; 
  26.   integer pxlmag ; 
  27.   int lastext = 0 ;
  28.   initialize () { integer i ; 
  29.     printf ( "This is GFread, C Version 0.0\n" ) ; 
  30.     for ( i = 0 ; i <= 639 ; i ++ ) mem [ i ] = 0 ; 
  31.       power [ 0 ] = 1 ; 
  32.     for ( i = 1 ; i <= 16 ; i ++ ) power [ i ] = power [ i - 1 ] * 2 ; 
  33.       } 
  34.   jumpout () { exit ( 1 ) ; 
  35.     } 
  36.   opengffile () { gffile = fopen ( gfname , "r" ) ; 
  37.     } 
  38.   openpxlfile () { pxlfile = fopen ( pxlname , "w" ) ; 
  39.     pxlloc = 0 ; 
  40.     } 
  41.   integer gfbyte () { integer i ; 
  42.     i = getc ( gffile ) ; 
  43.     if ( i < 0 ) return ( - i ) ; 
  44.     else return ( i ) ; 
  45.     } 
  46.   integer gfsignedquad () { integer i ; 
  47.     i = gfbyte () ; 
  48.     if ( i > 127 ) i = i - 256 ; 
  49.     i = 256 * i + gfbyte () ; 
  50.     i = 256 * i + gfbyte () ; 
  51.     return ( 256 * i + gfbyte () ) ; 
  52.     } 
  53.   pxlhalfword ( w ) 
  54.   integer w ; 
  55.   { if ( w < 0 ) w = w + 65536 ; 
  56.     { ptc ( w / 256 , pxlfile ) ; 
  57.       incr ( pxlloc ) ; 
  58.       } 
  59.     { ptc ( w % 256 , pxlfile ) ; 
  60.       incr ( pxlloc ) ; 
  61.       } 
  62.     } 
  63.   pxlword ( w ) 
  64.   integer w ; 
  65.   { if ( w > 0 ) 
  66.     { ptc ( w / 16777216 , pxlfile ) ; 
  67.       incr ( pxlloc ) ; 
  68.       } 
  69.     else 
  70.     { w = w + 1073741824 ; 
  71.       w = w + 1073741824 ; 
  72.       
  73.       { ptc ( ( w / 16777216 ) + 128 , pxlfile ) ; 
  74.         incr ( pxlloc ) ; 
  75.         } 
  76.       } 
  77.     
  78.     { ptc ( ( w / 65536 ) % 256 , pxlfile ) ; 
  79.       incr ( pxlloc ) ; 
  80.       } 
  81.     
  82.     { ptc ( ( w / 256 ) % 256 , pxlfile ) ; 
  83.       incr ( pxlloc ) ; 
  84.       } 
  85.     
  86.     { ptc ( w % 256 , pxlfile ) ; 
  87.       incr ( pxlloc ) ; 
  88.       } 
  89.     } 
  90.   loadgffile () { integer gfchar ; 
  91.     integer i, j, k ; 
  92.     integer gfcom ; 
  93.     integer minm, maxm ; 
  94.     integer minn, maxn ; 
  95.     integer rows ; 
  96.     boolean on ; 
  97.     boolean state ; 
  98.     integer extra ; 
  99.     boolean bad ; 
  100.     integer maxrows ; 
  101.     integer wordwidth ; 
  102.     integer word ; 
  103.     integer count ; 
  104.     integer bit ; 
  105.     integer hppp, vppp ; 
  106.     integer dx, dy ; 
  107.     opengffile () ; 
  108.     if ( gfbyte () != 247 ) 
  109.     { printf ( " Bad GF file: First byte is not preamble!\n" ) ; 
  110.       jumpout () ; 
  111.       } 
  112.     if ( gfbyte () != 131 ) 
  113.     { printf ( " Bad GF file: Identification byte is incorrect!\n" ) ;
  114.       jumpout () ; 
  115.       } 
  116.     i = gfbyte () ; 
  117.     for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ; 
  118.       do { gfcom = gfbyte () ; 
  119.       switch ( gfcom ) 
  120.       { case 67 : 
  121.         case 68 : 
  122.         
  123.         { if ( gfcom == 67 ) 
  124.           { gfch = gfsignedquad () ; 
  125.             i = gfsignedquad () ; 
  126.             minm = gfsignedquad () ; 
  127.             maxm = gfsignedquad () ; 
  128.             minn = gfsignedquad () ; 
  129.             maxn = gfsignedquad () ; 
  130.             } 
  131.           else 
  132.           { gfch = gfbyte () ; 
  133.             i = gfbyte () ; 
  134.             maxm = gfbyte () ; 
  135.             minm = maxm - i ; 
  136.             i = gfbyte () ; 
  137.             maxn = gfbyte () ; 
  138.             minn = maxn - i ; 
  139.             } 
  140.           if ( gfch > 127 ) do { gfcom = gfbyte () ; 
  141.             switch ( gfcom ) 
  142.             { case 239 : 
  143.               case 240 : 
  144.               case 241 : 
  145.               case 242 : 
  146.               
  147.               { i = 0 ; 
  148.                 for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte 
  149.                   () ; 
  150.                   for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ; 
  151.                   } 
  152.               break ; 
  153.               case 243 : 
  154.               k = gfsignedquad () ; 
  155.               break ; 
  156.               case 66 :
  157.               case 73 :
  158.               i = gfbyte () ;
  159.               case 65 :
  160.               case 72 :
  161.               i = gfbyte () ;
  162.               case 64 : 
  163.               case 71 : 
  164.               i = gfbyte () ; 
  165.               break ; 
  166.               default : ;
  167.               } } while ( ! ( gfcom == 69 ) ) ; 
  168.           else 
  169.           { maxrows = memmax - 640 ; 
  170.             bad = false ; 
  171.             rows = 0 ; 
  172.             on = false ; 
  173.             extra = 0 ; 
  174.             state = true ; 
  175.             do { gfcom = gfbyte () ; 
  176.               if ( gfcom >= 1 && gfcom <= 66 )
  177.                 { if ( gfcom < 64 ) i = gfcom - 0 ; 
  178.                   else 
  179.                   { i = 0 ; 
  180.                     for ( j = 0 ; j <= gfcom - 64 ; j ++ ) i = i * 256 + 
  181.                       gfbyte () ; 
  182.                       } 
  183.                   if ( state ) 
  184.                   { extra = extra + i ; 
  185.                     state = false ; 
  186.                     } 
  187.                   else 
  188.                   { 
  189.                     { if ( rows > maxrows ) bad = true ; 
  190.                       else 
  191.                       { mem [ rows + 640 ] = extra ; 
  192.                         incr ( rows ) ; 
  193.                         } 
  194.                       } 
  195.                     extra = i ; 
  196.                     } 
  197.                   on = ! on ; 
  198.                   } 
  199.               else if ( gfcom >= 74 && gfcom <= 238 )
  200.                 { if ( ! on && ( extra > 0 ) ) 
  201.                   { if ( rows > maxrows ) bad = true ; 
  202.                     else 
  203.                     { mem [ rows + 640 ] = extra ; 
  204.                       incr ( rows ) ; 
  205.                       } 
  206.                     } 
  207.                   
  208.                   { if ( rows > maxrows ) bad = true ; 
  209.                     else 
  210.                     { mem [ rows + 640 ] = 65535 ; 
  211.                       incr ( rows ) ; 
  212.                       } 
  213.                     } 
  214.                   on = true ; 
  215.                   extra = gfcom - 74 ; 
  216.                   state = false ; 
  217.                   } 
  218.             else  
  219.               switch ( gfcom ) 
  220.               { case 0 : 
  221.                 { state = ! state ; 
  222.                   on = ! on ; 
  223.                   } 
  224.                 break ; 
  225.                 case 70 : 
  226.                 case 71 : 
  227.                 case 72 : 
  228.                 case 73 : 
  229.                 { i = 0 ; 
  230.                   for ( j = 1 ; j <= gfcom - 70 ; j ++ ) i = i * 256 + gfbyte 
  231.                     () ; 
  232.                     if ( ! on && ( extra > 0 ) ) 
  233.                   { if ( rows > maxrows ) bad = true ; 
  234.                     else 
  235.                     { mem [ rows + 640 ] = extra ; 
  236.                       incr ( rows ) ; 
  237.                       } 
  238.                     } 
  239.                   for ( j = 0 ; j <= i ; j ++ ) 
  240.                     { if ( rows > maxrows ) bad = true ; 
  241.                       else 
  242.                       { mem [ rows + 640 ] = 65535 ; 
  243.                         incr ( rows ) ; 
  244.                         } 
  245.                       } 
  246.                     on = false ; 
  247.                   extra = 0 ; 
  248.                   state = true ; 
  249.                   } 
  250.                 case 239 : 
  251.                 case 240 : 
  252.                 case 241 : 
  253.                 case 242 : 
  254.                 { i = 0 ; 
  255.                   for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + 
  256.                     gfbyte () ; 
  257.                     for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ; 
  258.                     } 
  259.                 break ; 
  260.                 case 243 : 
  261.                 k = gfsignedquad () ; 
  262.                 break ; 
  263.                 case 247 : 
  264.                 ; 
  265.                 break ; 
  266.                 case 69 : 
  267.                 { if ( bad ) 
  268.                   { printf ( 
  269.                     " Ran out of internal memory for row counts!" ) ; 
  270.                     jumpout () ; 
  271.                     } 
  272.                   if ( ! on && ( extra > 0 ) ) 
  273.                   { if ( rows > maxrows ) bad = true ; 
  274.                     else 
  275.                     { mem [ rows + 640 ] = extra ; 
  276.                       incr ( rows ) ; 
  277.                       } 
  278.                     } 
  279.                   if ( ( rows > 0 ) && ( mem [ rows + 639 ] != 65535 ) ) 
  280.                   { if ( rows > maxrows ) bad = true ; 
  281.                     else 
  282.                     { mem [ rows + 640 ] = 65535 ; 
  283.                       incr ( rows ) ; 
  284.                       } 
  285.                     } 
  286.                   { if ( rows > maxrows ) bad = true ; 
  287.                     else 
  288.                     { mem [ rows + 640 ] = 65534 ; 
  289.                       incr ( rows ) ; 
  290.                       } 
  291.                     } 
  292.                   i = 0 ; 
  293.                   decr ( rows ) ; 
  294.                   mem [ gfch + 512 ] = pxlloc / 4 ; 
  295.                   while ( mem [ i + 640 ] == 65535 ) incr ( i ) ; 
  296.                     if ( mem [ i + 640 ] != 65534 ) 
  297.                   { maxn = maxn - i ; 
  298.                     while ( mem [ rows + 638 ] == 65535 ) 
  299.                       { decr ( rows ) ; 
  300.                         mem [ rows + 640 ] = 65534 ; 
  301.                         } 
  302.                       minn = maxn + 1 ; 
  303.                     extra = maxm - minm + 1 ; 
  304.                     maxm = 0 ; 
  305.                     j = i ; 
  306.                     while ( mem [ j + 640 ] != 65534 ) 
  307.                       { decr ( minn ) ; 
  308.                         if ( mem [ j + 640 ] != 65535 ) 
  309.                         { k = mem [ j + 640 ] ; 
  310.                           if ( k < extra ) extra = k ; 
  311.                           incr ( j ) ; 
  312.                           while ( mem [ j + 640 ] != 65535 ) 
  313.                             { k = k + mem [ j + 640 ] ; 
  314.                               incr ( j ) ; 
  315.                               } 
  316.                             if ( maxm < k ) maxm = k ; 
  317.                           } 
  318.                         incr ( j ) ; 
  319.                         } 
  320.                       minm = minm + extra ; 
  321.                     maxm = minm + maxm - 1 ; 
  322.                     mem [ gfch + 128 ] = maxn - minn + 1 ; 
  323.                     mem [ gfch ] = maxm - minm + 1 ; 
  324.                     mem [ gfch + 256 ] = - minm ; 
  325.                     mem [ gfch + 384 ] = maxn ; 
  326.                     wordwidth = ( maxm - minm + 32 ) / 32 * 2 ; 
  327.                     while ( mem [ i + 640 ] != 65534 ) 
  328.                       { j = 0 ; 
  329.                         word = 0 ; 
  330.                         bit = 16 ; 
  331.                         on = false ; 
  332.                         count = mem [ i + 640 ] - extra ; 
  333.                         while ( count != 65535 ) 
  334.                           { incr ( i ) ; 
  335.                             while ( count > 0 ) 
  336.                               { if ( count >= bit ) 
  337.                                 { if ( on ) word = word + power [ bit ] - 1 ; 
  338.                                   count = count - bit ; 
  339.                                   pxlhalfword ( word ) ; 
  340.                                   incr ( j ) ; 
  341.                                   word = 0 ; 
  342.                                   bit = 16 ; 
  343.                                   } 
  344.                                 else 
  345.                                 { if ( on ) word = word + power [ bit ] - 
  346.                                   power [ bit - count ] ; 
  347.                                   bit = bit - count ; 
  348.                                   count = 0 ; 
  349.                                   } 
  350.                                 } 
  351.                               on = ! on ; 
  352.                             count = mem [ i + 640 ] ; 
  353.                             } 
  354.                           while ( j < wordwidth ) 
  355.                           { pxlhalfword ( word ) ; 
  356.                             word = 0 ; 
  357.                             incr ( j ) ; 
  358.                             } 
  359.                           incr ( i ) ; 
  360.                         } 
  361.                       } 
  362.                   } 
  363.                 break ; 
  364.                 } } while ( ! ( gfcom == 69 ) ) ; 
  365.             } 
  366.           } 
  367.         break ; 
  368.         case 239 : 
  369.         case 240 : 
  370.         case 241 : 
  371.         case 242 : 
  372.         { i = 0 ; 
  373.           for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte () ; 
  374.             for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ; 
  375.             } 
  376.         break ; 
  377.         case 243 : 
  378.         k = gfsignedquad () ; 
  379.         break ; 
  380.         case 247 : 
  381.         ; 
  382.         break ; 
  383.         case 248 : 
  384.         ; 
  385.         break ; 
  386.         default : 
  387.         { printf ( 
  388.   " Bad GF file: Unexpected %d command between characters!\n" , gfcom ) ; 
  389.           jumpout () ; 
  390.           } 
  391.         break ; 
  392.         } } while ( ! ( gfcom == 248 ) ) ; 
  393.     for ( gfch = 0 ; gfch <= 127 ; gfch ++ ) mem [ gfch + 640 ] = 0 ; 
  394.       i = gfsignedquad () ; 
  395.     designsize = gfsignedquad () ; 
  396.     checksum = gfsignedquad () ; 
  397.     hppp = gfsignedquad () ; 
  398.     vppp = gfsignedquad () ; 
  399.     if ( hppp != vppp ) printf ( "Odd aspect ratio!\n" ) ; 
  400.     pxlmag = round ( ( ( 5 * hppp ) * 72.27 ) / 65536.0 ) ; 
  401.     i = gfsignedquad () ; 
  402.     i = gfsignedquad () ; 
  403.     i = gfsignedquad () ; 
  404.     i = gfsignedquad () ; 
  405.     do { gfcom = gfbyte () ; 
  406.       switch ( gfcom ) 
  407.       { case 245 : 
  408.         case 246 : 
  409.         { gfch = gfbyte () ; 
  410.           if ( gfcom == 245 ) 
  411.           { dx = gfsignedquad () ; 
  412.             dy = gfsignedquad () ; 
  413.             } 
  414.           else 
  415.           { dx = gfbyte () * 65536 ; 
  416.             dy = 0 ; 
  417.             } 
  418.           mem [ gfch + 640 ] = gfsignedquad () ; 
  419.           i = gfsignedquad () ; 
  420.           } 
  421.         break ; 
  422.         case 239 : 
  423.         case 240 : 
  424.         case 241 : 
  425.         case 242 : 
  426.         
  427.         { i = 0 ; 
  428.           for ( j = 0 ; j <= gfcom - 239 ; j ++ ) i = i * 256 + gfbyte () ; 
  429.             for ( j = 1 ; j <= i ; j ++ ) k = gfbyte () ; 
  430.             } 
  431.         break ; 
  432.         case 243 : 
  433.         k = gfsignedquad () ; 
  434.         break ; 
  435.         case 247 : 
  436.         ; 
  437.         break ; 
  438.         case 249 : 
  439.         ; 
  440.         break ; 
  441.         default : 
  442.         
  443.         { printf ( " Bad GF file: Unexpected %d in postamble!\n" , gfcom ) ;
  444.           jumpout () ; 
  445.           } 
  446.         break ; 
  447.         } } while ( ! ( gfcom == 249 ) ) ; 
  448.     } 
  449.   main (argc, argv) 
  450. int argc;
  451. char *argv[] ;
  452. { if ( argc < 2 || argc > 3 )
  453.   { printf("Usage: gfread gffile[.gf] [pxlfile[.nnnnpxl]]\n") ;
  454.     jumpout () ; }
  455.   { int i ;
  456.     strcpy(gfname, argv[1]);
  457.     for (i=0;gfname[i]!=0;i++)
  458.       if ( gfname[i] == '/' ) lastext = 0 ;
  459.       else if ( gfname[i] == '.' && lastext == 0 ) lastext = i ;
  460.     strcpy(pxlname, gfname) ;
  461.     if ( lastext == 0 ) {
  462.       strcpy(gfname+i, ".gf") ;
  463.       lastext =i ; }
  464.     strcpy(pxlname+lastext, ".pxl") ;
  465.     if (argc == 3 ) strcpy(pxlname, argv[2]) ; }
  466.   initialize () ; 
  467.   openpxlfile () ; 
  468.   pxlword ( 1001 ) ; 
  469.   loadgffile () ; 
  470.   dirptr = pxlloc / 4 ; 
  471.   for ( gfch = 0 ; gfch <= 127 ; gfch ++ ) 
  472.     { pxlhalfword ( mem [ gfch ] ) ; 
  473.       pxlhalfword ( mem [ gfch + 128 ] ) ; 
  474.       pxlhalfword ( mem [ gfch + 256 ] ) ; 
  475.       pxlhalfword ( mem [ gfch + 384 ] ) ; 
  476.       pxlword ( mem [ gfch + 512 ] ) ; 
  477.       pxlword ( mem [ gfch + 640 ] ) ; 
  478.       } 
  479.     pxlword ( checksum ) ; 
  480.   pxlword ( pxlmag ) ; 
  481.   pxlword ( designsize ) ; 
  482.   pxlword ( dirptr ) ; 
  483.   pxlword ( 1001 ) ; 
  484.   fclose(pxlfile) ;
  485.   if ( argc == 2 ) {
  486.     strcpy(gfname, pxlname) ;
  487.     sprintf(gfname+lastext, ".%dpxl", pxlmag) ;
  488.     rename(pxlname, gfname) ; }
  489.   lab9999 : ; 
  490.   } 
  491.